package com.leo.chapter15;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 用显式锁实现随机数生成器
 * @author xuexiaolei
 * @version 2017年10月20日
 */
public class ReentrantLockPseudoRandom {
    private final Lock lock = new ReentrantLock(false);
    private int seed;

    public ReentrantLockPseudoRandom(int seed) {
        this.seed = seed;
    }

    public int nextInt(int n){
        lock.lock();
        try {
            int s = seed;
            seed = caclulateNext(s);
            int remainder = s % n;
            return remainder > 0? remainder : remainder+n;
        } finally {
            lock.unlock();
        }
    }

    private int caclulateNext(int s) {
        return 0;
    }
}
